package cn.flying.cloud.task.core.quartz.job;//package cn.flying.cloud.task.quartz.job;
//
//import java.time.Duration;
//import java.time.LocalDateTime;
//
//import org.apache.commons.lang3.exception.ExceptionUtils;
//import org.quartz.JobDataMap;
//import org.quartz.JobExecutionContext;
//import org.quartz.JobExecutionException;
//import org.springframework.beans.BeanUtils;
//import org.springframework.context.ApplicationContext;
//import org.springframework.scheduling.quartz.QuartzJobBean;
//
//import cn.flying.cloud.base.common.utils.Ft;
//import cn.flying.cloud.task.core.manager.domain.SysJobInfo;
//import cn.flying.cloud.task.core.manager.domain.SysJobLog;
//import cn.flying.cloud.task.core.manager.service.SysJobLogService;
//import cn.flying.cloud.task.core.quartz.constants.TaskConstant;
//
///**
// *  任务处理抽象类
// * @author: admin
// * @date: 2024年01月26日 15:23
// * @version: 1.0
// */
//public abstract class AbstractScheduleQuartzJob extends QuartzJobBean {
//
//    /**
//     * 线程本地变量
//     */
//    private static ThreadLocal<LocalDateTime> threadLocal = new ThreadLocal<>();
//
//    /**
//     * Execute the actual job. The job data map will already have been
//     * applied as bean property values by execute. The contract is
//     * exactly the same as for the standard Quartz execute method.
//     *
//     * @param context
//     * @see #execute
//     */
//    @Override
//    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
//        SysJobInfo jobInfo = new SysJobInfo();
//
//        JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
//        BeanUtils.copyProperties(mergedJobDataMap.get(TaskConstant.JOB_PERSISTENCE), jobInfo);
//        ApplicationContext applicationContext = Ft.spring.getApplicationContext();
//        AbstractScheduleQuartzJob abstractScheduleQuartzJob = applicationContext.getBeansOfType(AbstractScheduleQuartzJob.class).get(jobInfo.getJobTarget());
//
//        before(context, jobInfo);
//
//        try {
//            abstractScheduleQuartzJob.doExecute(context, jobInfo);
//            after(context, jobInfo, null);
//        } catch (Exception e) {
//            after(context, jobInfo, e);
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * 执行前
//     *
//     * @param context 工作执行上下文对象
//     * @param jobInfo 系统计划任务
//     */
//    protected void before(JobExecutionContext context, SysJobInfo jobInfo) {
//        threadLocal.set(LocalDateTime.now());
//    }
//
//    /**
//     * 执行后
//     *
//     * @param context 工作执行上下文对象
//     * @param jobInfo 系统计划任务
//     */
//    protected void after(JobExecutionContext context, SysJobInfo jobInfo, Exception e) {
//        LocalDateTime startTime = threadLocal.get();
//        LocalDateTime endTime = LocalDateTime.now();
//        Duration duration = Duration.between(startTime, endTime);
//
//        SysJobLog log = new SysJobLog();
//        log.setJobName(jobInfo.getJobName());
//        log.setJobGroup(jobInfo.getJobGroup());
//        log.setJobTarget(jobInfo.getJobTarget());
//        log.setJobCron(jobInfo.getJobCron());
//        log.setJobDesc(jobInfo.getJobDesc());
//        log.setJobParam(jobInfo.getJobParam());
//        log.setStatus(jobInfo.getStatus());
//        log.setDuration((int) duration.getSeconds());
//        log.setJobMsg("执行成功！");
//        log.setJobTrace("");
//        log.setSchedTime(startTime);
//        log.setExecTime(startTime);
//        log.setCreatedBy("system");
//        log.setCreatedTime(LocalDateTime.now());
//        log.setUpdatedBy("system");
//        log.setUpdatedTime(LocalDateTime.now());
//        log.setDeleted("0");
//
//        if (e != null) {
//            log.setJobMsg("执行失败！" + e.getMessage());
//            log.setJobTrace(ExceptionUtils.getStackTrace(e));
//        }
//
//
//        ((SysJobLogService) Ft.spring.getBean("sysJobLogServiceImpl")).save(log);
//    }
//
//    /**
//     * 执行任务
//     *
//     * @param context
//     * @param jobInfo
//     */
//    protected abstract void doExecute(JobExecutionContext context, SysJobInfo jobInfo) throws Exception;
//}
